<?php

namespace Drinkiit\BlogBundle\Handler;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;

use Drinkiit\BlogBundle\Entity\Produit;

class AdminProduitListeHandler
{
    protected $form;
    protected $request;
    protected $em;
    protected $controller;

    public function __construct(Form $form, Request $request, Controller $controller, EntityManager $em)
    {
        $this->form    = $form;
        $this->request = $request;
        $this->em      = $em;
        $this->controller = $controller ;
    }

    public function process()
    {
        if( $this->request->getMethod() == 'POST' )
        {
            $this->form->bindRequest($this->request);

            if( $this->form->isValid() and $this->request->query->has('id') )
            {
				$id = $this->request->query->get('id') ;
				$produit = $this->em->getRepository('DrinkiitBlogBundle:Produit')->find($id) ;
				
				$produit->setDescription( $this->form->get('description')->getData() ) ;
				$produit->setDisponibilite( $this->form->get('disponibilite')->getData() ) ;
				$produit->setPrix( $this->form->get('prix')->getData() * 100 ) ;
				
				$this->em->flush() ;
				
				$this->controller->get('logger')->info("[INFO] On a bien modifié le produit n°".$this->form->getData()->getId());
				
                return true;
            }
        }

        return false;
    }
}
